#ifndef CONNECTIONWIDGET_H
#define CONNECTIONWIDGET_H

#include <DWidget>
#include <QSqlDatabase>
#include <QSqlQueryModel>
#include <DTreeWidget>
#include <DTableView>
#include <DTextEdit>

DWIDGET_USE_NAMESPACE

class ConnectionWidget : public DWidget
{
    Q_OBJECT
public:
    explicit ConnectionWidget(QWidget *parent = nullptr);
    ~ConnectionWidget();

private slots:
    void addConnection();
    void editConnection();
    void removeConnection();
    void testConnection();
    void connectToDatabase();
    void disconnectFromDatabase();
    void refreshDatabaseObjects();
    void executeQuery();
    void showContextMenu(const QPoint &pos);
    void handleTreeItemDoubleClicked(QTreeWidgetItem *item, int column);

private:
    void setupUI();
    void setupConnections();
    void loadConnections();
    void saveConnections();
    void updateConnectionStatus(const QString &name, bool connected);
    void loadDatabaseObjects(const QString &connectionName);
    void addTableNode(QTreeWidgetItem *parent, const QString &tableName);
    
    // UI elements
    QSplitter *mainSplitter;
    QSplitter *rightSplitter;
    
    // Left panel - Connection list
    DTreeWidget *connectionsTree;
    DPushButton *addBtn;
    DPushButton *editBtn;
    DPushButton *removeBtn;
    DPushButton *testBtn;
    DPushButton *connectBtn;
    DPushButton *disconnectBtn;
    DPushButton *refreshBtn;
    
    // Right panel - Query editor and results
    DTextEdit *queryEditor;
    DTableView *resultsView;
    DPushButton *executeBtn;
    
    QMap<QString, QSqlDatabase> connections;
    QSqlQueryModel *queryModel;
};

#endif // CONNECTIONWIDGET_H